﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi.AppExtract
{
    /// <summary>
    /// 获取被拒绝的数据明细
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("AppExtract", "GetRejectedDataDetail", "1.0")]
    public class GetRejectedDataDetailAction : BaseAction<GetRejectedDataDetailParams, GetRejectedDataDetailResult>
    {
        protected override ActionResult<GetRejectedDataDetailResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetRejectedDataDetailParams opParams)
        {
            SER.AppExtractService service = new SER.AppExtractService();
            ServiceResult<VIW.VRawDataDetail> serResult = service.GetDataDetail(opParams.BatchId, opParams.ServiceType, opParams.Source, opParams.DataId);
            if (serResult.IsSuccess)
            {
                GetRejectedDataDetailResult actReuslt = new GetRejectedDataDetailResult();
                actReuslt.DataId = serResult.Data.DataId;
                actReuslt.BatchId = serResult.Data.BatchId;
                actReuslt.DataIdentity = serResult.Data.DataIdentity;
                actReuslt.DtServiceId = serResult.Data.DtServiceId;
                actReuslt.DtServiceText = serResult.Data.DtServiceText;
                actReuslt.DtSourceId = serResult.Data.DtSourceId;
                actReuslt.DtSourceText = serResult.Data.DtSourceText;
                actReuslt.SourceText = serResult.Data.SourceText;
                actReuslt.Source = serResult.Data.Source;
                actReuslt.ServiceType = serResult.Data.ServiceType;
                actReuslt.ServiceText = serResult.Data.ServiceText;
                actReuslt.CreatedTime = serResult.Data.CreatedTime;
                for (int index = 0; index < serResult.Data.Parameters.Count; index++)
                {
                    GetRejectedDataDetailResult.Parameter parameter = new GetRejectedDataDetailResult.Parameter();
                    parameter.Name = serResult.Data.Parameters[index].DtSourceParameterName;
                    parameter.Value = serResult.Data.Parameters[index].ValueString;
                    actReuslt.Parameters.Add(parameter);
                }
                return new ActionResult<GetRejectedDataDetailResult>(actReuslt);
            }
            else
            {
                return new ActionResult<GetRejectedDataDetailResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetRejectedDataDetailParams
    {
        /// <summary>
        /// 批次id
        /// </summary>
        public Guid BatchId { get; set; }

        /// <summary>
        /// 数据服务类型
        /// </summary>
        public Guid ServiceType { get; set; }
        /// <summary>
        /// 数据源
        /// </summary>
        public Guid Source { get; set; }

        public Guid DataId { get; set; }
    }

    public class GetRejectedDataDetailResult
    {
        public Guid DataId { get; set; }

        /// <summary>
        /// 这条数据被采集过来的时候的批次号，而不是外部系统采集他时的批次号
        /// </summary>
        public Guid BatchId { get; set; }

        /// <summary>
        /// 外部程序抽取数据时对应的数据源信息（这个就是DtTableId）
        /// </summary>
        public Guid Source { get; set; }

        /// <summary>
        /// 外部程序抽取数据时对应的数据源名称（这个就是DtTable的名称）
        /// </summary>
        public string SourceText { get; set; }

        public Guid ServiceType { get; set; }

        public string ServiceText { get; set; }

        /// <summary>
        /// 本系统内部向数据表抽数的数据服务Id
        /// </summary>
        public Guid DtServiceId { get; set; }
        /// <summary>
        /// 本系统内部向数据表抽数的数据服务名称
        /// </summary>
        public string DtServiceText { get; set; }

        /// <summary>
        /// 本系统内部向数据表抽数的数据源id
        /// </summary>
        public Guid DtSourceId { get; set; }

        /// <summary>
        /// 本系统内部向数据表抽数的数据源名称
        /// </summary>
        public string DtSourceText { get; set; }

        public string DataIdentity { get; set; }
        public DateTime CreatedTime { get; set; }

        public List<GetRejectedDataDetailResult.Parameter> Parameters { get; set; }

        public GetRejectedDataDetailResult()
        {
            Parameters = new List<GetRejectedDataDetailResult.Parameter>();
        }

        public class Parameter
        {
            public string Name { get; set; }

            public string Value { get; set; }
        }
    }
}
